package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SetArea;
import com.ruoyi.system.domain.SetLine;
import com.ruoyi.system.mapper.SetAreaMapper;
import com.ruoyi.system.mapper.SetLineMapper;
import com.ruoyi.system.service.ISetLineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 线体Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-21
 */
@Service
public class SetLineServiceImpl implements ISetLineService
{
    private static final Logger log = LoggerFactory.getLogger(SetLineServiceImpl.class);

    @Autowired
    private SetLineMapper setLineMapper;

    @Autowired
    private SetAreaMapper setAreaMapper;

    /**
     * 查询线体
     * 
     * @param id 线体主键
     * @return 线体
     */
    @Override
    public SetLine selectSetLineById(Long id)
    {
        return setLineMapper.selectSetLineById(id);
    }

    /**
     * 查询线体
     *
     * @param lineCode 线体编码
     * @return 线体
     */
    public SetLine selectSetLineByLineCode(String lineCode)
    {
        return setLineMapper.selectSetLineByLineCode(lineCode);
    }

    /**
     * 查询线体列表
     * 
     * @param setLine 线体
     * @return 线体
     */
    @Override
    public List<SetLine> selectSetLineList(SetLine setLine)
    {
        return setLineMapper.selectSetLineList(setLine);
    }

    /**
     * 新增线体
     * 
     * @param setLine 线体
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSetLine(SetLine setLine)
    {
        setLine.setCreateTime(DateUtils.getNowDate());
        setLine.setCreateBy(SecurityUtils.getUsername());
        setLine.setStatus(UserConstants.NORMAL);
        return setLineMapper.insertSetLine(setLine);
    }

    /**
     * 修改线体
     * 
     * @param setLine 线体
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSetLine(SetLine setLine)
    {
        setLine.setUpdateTime(DateUtils.getNowDate());
        setLine.setUpdateBy(SecurityUtils.getUsername());
        return setLineMapper.updateSetLine(setLine);
    }

    /**
     * 批量删除线体
     *
     * @param ids 需要删除的线体主键
     * @return 结果
     */
    @Override
    public int deleteSetLineByIds(Long[] ids) {
        return setLineMapper.deleteSetLineByIds(ids);
    }

    /**
     * 删除线体信息
     * 
     * @param id 线体主键
     * @return 结果
     */
    @Override
    public int deleteSetLineById(Long id)
    {
        return setLineMapper.deleteSetLineById(id);
    }

    /**
     * 导入线体数据
     *
     * @param lineList 线体数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importLine(List<SetLine> lineList, boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(lineList) || lineList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (SetLine setLine : lineList)
        {
            try
            {
                // 验证是否存在这个线体
                SetLine sl = setLineMapper.selectSetLineByLineCode(setLine.getLineCode());
                SetArea sa = setAreaMapper.selectSetAreaByAreaCode(setLine.getAreaCode());
                if(StringUtils.isNull(sa)){
                    failureNum++;
                    failureMsg.append("<br/> " + failureNum + "、线体 " + setLine.getLineName() + "的区域编码" + setLine.getAreaCode() + " 不存在");
                }else{
                    if (StringUtils.isNull(sl))
                    {
                        setLine.setCreateBy(operName);
                        this.insertSetLine(setLine);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、线体 " + setLine.getLineName() + " 导入成功");
                    }
                    else if (isUpdateSupport)
                    {
                        setLine.setUpdateBy(operName);
                        this.updateSetLine(setLine);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、线体 " + setLine.getLineName() + " 更新成功");
                    }
                    else
                    {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、线体 " + setLine.getLineName() + " 已存在");
                    }
                }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、线体 " + setLine.getLineName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 校验产线编码是否唯一
     *
     * @param setLine 产线信息
     * @return 结果
     */
    @Override
    public String checkCodeUnique(SetLine setLine)
    {
        Long id = StringUtils.isNull(setLine.getId()) ? -1L : setLine.getId();
        SetLine info = setLineMapper.checkCodeUnique(setLine.getLineCode());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验产线名称是否唯一
     *
     * @param setLine 产线信息
     * @return 结果
     */
    @Override
    public String checkNameUnique(SetLine setLine)
    {
        Long id = StringUtils.isNull(setLine.getId()) ? -1L : setLine.getId();
        SetLine info = setLineMapper.checkNameUnique(setLine.getLineName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
